home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 114_01 / ed3.bds < prev    next >
Text File  |  1987-07-13  |  14KB  |  713 lines

  1. /*
  2.  * Screen editor:  command mode commands -- enhanced
  3.  *
  4.  * Source: ed3.bds
  5.  * Version: December 20, 1981.
  6.  * Transliteration of small-C version of September 5, 1981
  7.  *
  8.  */
  9.  
  10. /* define globals */
  11.  
  12. #include ed.h
  13. #include bdscio.h
  14. #include ed1.ccc
  15. #include edext.cc
  16.  
  17. /* data global to these routines */
  18.  
  19. /* comment out -----
  20. char filename [SYSFNMAX];
  21. ----- end comment out */
  22.  
  23.  
  24. /* append command.
  25.  * load a file into main buffer at current location.
  26.  * this command does NOT change the current file name.
  27.  */
  28.  
  29. append(args) char *args;
  30. {
  31. char buffer [MAXLEN];        /* disk line buffer */
  32. int file;
  33. int n;
  34. int topline;
  35. char locfn [SYSFNMAX];        /* local file name */
  36.     /* get file name which follows command */
  37.     if (name1(args,locfn) == ERR) {
  38.         return;
  39.     }
  40.     if (locfn [0] == EOS) {
  41.         message("no file argument");
  42.         return;
  43.     }
  44.     /* open the new file */
  45.     if ((file=sysopen(locfn,"r")) == ERR) {
  46.         message("file not found");
  47.         return;
  48.     }
  49.     /* read the file into the buffer */
  50.     while ((n=readline(file,buffer,MAXLEN)) >= 0) {
  51.         if (n > MAXLEN) {
  52.             message("line truncated");
  53.             n=MAXLEN;
  54.         }
  55.         if (bufins(buffer,n) == ERR) {
  56.             break;
  57.         }
  58.         if (bufdn() == ERR) {
  59.             break;
  60.         }
  61.     }
  62.     /* close the file */
  63.     sysclose(file);
  64.     /* redraw the screen so topline will be at top
  65.      * of the screen after command() does a CR/LF.
  66.      */
  67.     topline=max(1,bufln()-SCRNL2);
  68.     bufout(topline,2,SCRNL2);
  69.     bufgo(topline);
  70. }
  71.  
  72. /* global change command */
  73.  
  74. change(args) char *args;
  75. {
  76. char oldline [MAXLEN1];        /* reserve space for EOS */
  77. char newline [MAXLEN1];
  78. char oldpat [MAXLEN1];
  79. char newpat [MAXLEN1];
  80. int from, to, col, n, k;
  81.     if (get2args(args,&from,&to) == ERR) {
  82.         return;
  83.     }
  84.     /* get search and change masks into oldpat, newpat */
  85.     fmtsout("search mask ?  ",0);
  86.     getcmnd(oldpat,15);
  87.     fmtcrlf();
  88.     if (oldpat [0] == EOS) {
  89.         return;
  90.     }
  91.     pmtline();
  92.     fmtsout("change mask ?  ",0);
  93.     getcmnd(newpat,15);
  94.     fmtcrlf();
  95.     /* make substitution for lines between from, to */
  96.     while (from <= to) {
  97.         if (chkkey() == YES) {
  98.             break;
  99.         }
  100.         if (bufgo(from++) == ERR) {
  101.             break;
  102.         }
  103.         if (bufatbot() == YES) {
  104.             break;
  105.         }
  106.         n=bufgetln(oldline,MAXLEN);
  107.         n=min(n,MAXLEN);
  108.         oldline [n]=EOS;
  109.         /* '^' anchors search */
  110.         if (oldpat [0] == '^') {
  111.             if (amatch(oldline,oldpat+1,0) == YES) {
  112.                 k=replace(oldline,newline,
  113.                     oldpat+1,newpat,0);
  114.                 if (k == ERR) {
  115.                     return;
  116.                 }
  117.                 fmtcrlf();
  118.                 putdec(bufln(),5);
  119.                 fmtsout(newline,5);
  120.                 outdeol();
  121.                 bufrepl(newline,k);
  122.             }
  123.             continue;
  124.         }
  125.         /* search oldline for oldpat */
  126.         col=0;
  127.         while (col < n) {
  128.             if (amatch(oldline,oldpat,col++) == YES){
  129.                 k=replace(oldline,newline,
  130.                     oldpat,newpat,col-1);
  131.                 if (k == ERR) {
  132.                     return;
  133.                 }
  134.                 fmtcrlf();
  135.                 putdec(bufln(),5);
  136.                 fmtsout(newline,5);
  137.                 outdeol();
  138.                 bufrepl(newline,k);
  139.                 break;
  140.             }
  141.         }
  142.     }
  143.     fmtcrlf();
  144. }
  145.  
  146. /* clear main buffer and file name */
  147.  
  148. clear()
  149. {
  150.     /* make sure it is ok to clear buffer */
  151.     if (chkbuf() == YES) {
  152.         filename [0]=0;
  153.         pmtfile("");
  154.         outclr();
  155.         outxy(0,SCRNL1);
  156.         bufnew();
  157.         message("buffer cleared");
  158.     }
  159. }
  160.  
  161. /* multiple line delete command */
  162.  
  163. delete(args) char *args;
  164. {
  165. int from, to;
  166.     if (get2args(args,&from,&to) == ERR) {
  167.         return;
  168.     }
  169.     if (from > to) {
  170.         return;
  171.     }
  172.     /* go to first line to be deleted */
  173.     if (bufgo(from) == ERR) {
  174.         return;
  175.     }
  176.     /* delete all line between from and to */
  177.     if (bufdeln(to-from+1) == ERR) {
  178.         return;
  179.     }
  180.     /* redraw the screen */
  181.     bufout(bufln(),1,SCRNL1);
  182. }
  183.  
  184. /* search all lines below the current line for a pattern
  185.  * return -1 if pattern not found.
  186.  * otherwise, return column number of start of pattern.
  187.  */
  188.  
  189. find()
  190. {
  191.     return(search1(bufln()+1,HUGE,YES));
  192. }
  193.  
  194. /* list lines to list device */
  195.  
  196. list(args) char *args;
  197. {
  198. char linebuf [MAXLEN1];
  199. int n;
  200. int from, to, line, oldline;
  201.     /* save the buffer's current line */
  202.     oldline=bufln();
  203.     /* get starting, ending lines to print */
  204.     if (get2args(args,&from,&to) == ERR) {
  205.         return;
  206.     }
  207.     /* print lines one at a time to list device */
  208.     line=from;
  209.     while (line <= to) {
  210.         /* make sure prompt goes to console */
  211.         fmtassn(NO);
  212.         /* check for interrupt */
  213.         if (chkkey() == YES) {
  214.             break;
  215.         }
  216.         /* print line to list device */
  217.         fmtassn(YES);
  218.         if (bufgo(line++) != OK) {
  219.             break;
  220.         }
  221.         if (bufatbot()) {
  222.             break;
  223.         }
  224.         n=bufgetln(linebuf,MAXLEN1);
  225.         n=min(n,MAXLEN);
  226.         linebuf [n]=CR;
  227.         fmtsout(linebuf,0);
  228.         fmtcrlf();
  229.     }
  230.     /* redirect output to console */
  231.     fmtassn(NO);
  232.     /* restore cursor */
  233.     bufgo(oldline);
  234. }
  235.  
  236. /* load file into buffer */
  237.  
  238. load (args) char *args;
  239. {
  240. char buffer [MAXLEN];    /* disk line buffer */
  241. char locfn  [SYSFNMAX];  /* file name until we check it */
  242. int n;
  243. int file;
  244. int topline;
  245.     /* get filename following command */
  246.     if (name1(args,locfn) == ERR) {
  247.         return;
  248.     }
  249.     if (locfn [0] == EOS) {
  250.         message("no file argument");
  251.         return;
  252.     }
  253.     /* give user a chance to save the buffer */
  254.     if (chkbuf() == NO) {
  255.         return;
  256.     }
  257.     /* open the new file */
  258.     if ((file=sysopen(locfn,"r")) == ERR) {
  259.         message("file not found");
  260.         return;
  261.     }
  262.     /* update file name */
  263.     syscopfn(locfn, filename);
  264.     pmtfile(filename);
  265.     /* clear the buffer */
  266.     bufnew();
  267.     /* read the file into the buffer */
  268.     while ((n=readline(file,buffer,MAXLEN)) >= 0) {
  269.         if (n > MAXLEN) {
  270.             message("line truncated");
  271.             n=MAXLEN;
  272.         }
  273.         if (bufins(buffer,n) == ERR) {
  274.             break;
  275.         }
  276.         if (bufdn() == ERR) {
  277.             break;
  278.         }
  279.     }
  280.     /* close the file */
  281.     sysclose(file);
  282.     /* indicate that the buffer is fresh */
  283.     bufsaved();
  284.     /* set current line to line 1 */
  285.     bufgo(1);
  286.     /* redraw the screen so that topline will be
  287.      * on line 1 after command() does a CR/LF.
  288.      */
  289.     topline=max(1,bufln()-SCRNL2);
  290.     bufout(topline,2,SCRNL2);
  291.     bufgo(topline);
  292. }
  293.  
  294. /* change current file name */
  295.  
  296. name(args) char *args;
  297. {
  298.     name1(args,filename);
  299.     pmtfile(filename);
  300. }
  301.  
  302. /* check syntax of args.
  303.  * copy to filename.
  304.  * return OK if the name is valid.
  305.  */
  306.  
  307. name1(args,filename) char *args, *filename;
  308. {
  309.     /* skip command */
  310.     args=skiparg(args);
  311.     args=skipbl(args);
  312.     /* check file name syntax */
  313.     if (syschkfn(args) == ERR) {
  314.         return(ERR);
  315.     }
  316.     /* copy filename */
  317.     syscopfn(args,filename);
  318.     return(OK);
  319. }
  320.  
  321. /* save the buffer in an already existing file */
  322.  
  323. resave()
  324. {
  325. char linebuf [MAXLEN];
  326. int file, n, oldline;
  327.     /* make sure file has a name */
  328.     if (filename [0] == EOS) {
  329.         message("file not named");
  330.         return;
  331.     }
  332.     /* the file must exist for resave */
  333.     if ((file=sysopen(filename,"r")) == ERR) {
  334.         message("file not found");
  335.         return;
  336.     }
  337.     if (sysclose(file) == ERR) {
  338.         return;
  339.     }
  340.     /* open the file for writing */
  341.     if ((file=sysopen(filename,"w")) == ERR) {
  342.         return;
  343.     }
  344.     /* save the current position of file */
  345.     oldline=bufln();
  346.     /* write out the whole file */
  347.     if (bufgo(1) == ERR) {
  348.         sysclose(file);
  349.         return;
  350.     }
  351.     while (bufatbot() == NO) {
  352.         n=bufgetln(linebuf,MAXLEN);
  353.         n=min(n,MAXLEN);
  354.         if (pushline(file,linebuf,n) == ERR) {
  355.             break;
  356.         }
  357.         if (bufdn() == ERR) {
  358.             break;
  359.         }
  360.     }
  361.     /* indicate if all buffer was saved */
  362.     if (bufatbot()){
  363.         bufsaved();
  364.     }
  365.     /* close file and restore line number */
  366.     sysclose(file);
  367.     bufgo(oldline);
  368. }
  369.  
  370. /* save the buffer in a new file */
  371.  
  372. save()
  373. {
  374. char linebuf [MAXLEN];
  375. int file, n, oldline;
  376.     /* make sure the file is named */
  377.     if (filename [0] == EOS) {
  378.         message("file not named");
  379.         return;
  380.     }
  381.     /* file must NOT exist for save */
  382.     if ((file=sysopen(filename,"r")) != ERR) {
  383.         sysclose(file);
  384.         message("file exists");
  385.         return;
  386.     }
  387.     /* open file for writing */
  388.     if ((file=sysopen(filename,"w")) == ERR) {
  389.         return;
  390.     }
  391.     /* remember current line */
  392.     oldline=bufln();
  393.     /* write entire buffer to file */
  394.     if (bufgo(1) == ERR) {
  395.         sysclose(file);
  396.         return;
  397.     }
  398.     while (bufatbot() == NO) {
  399.         n=bufgetln(linebuf,MAXLEN);
  400.         n=min(n,MAXLEN);
  401.         if (pushline(file,linebuf,n) == ERR) {
  402.             break;
  403.         }
  404.         if (bufdn() == ERR) {
  405.             break;
  406.         }
  407.     }
  408.     /* indicate buffer saved if good write */
  409.     if (bufatbot()) {
  410.         bufsaved()